package com.shijiwei.lifthelper.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shijiwei.lifthelper.R;
import com.shijiwei.lifthelper.bean.DBJoke;
import com.shijiwei.lifthelper.bean.JokeBean;
import com.shijiwei.lifthelper.utils.DbUtils;

import org.xutils.ex.DbException;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by Administrator on 2015/12/13.
 */
public class JokeAdapter extends AppBaseAdapter {
    public JokeAdapter(List<JokeBean> list, Context context) {
        super(list, context);
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder ;
        if (convertView == null){
            convertView = inflater.inflate(R.layout.item_listview_joke,parent,false);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final JokeBean joke = (JokeBean) list.get(position);
        if (joke.getTitle().length()>0) {
            holder.titleText.setText(joke.getTitle());
        } else {
            holder.titleText.setVisibility(View.GONE);
        }
        holder.bodyText.setText(joke.getContent().replaceAll("&hellip;","."));
        holder.agreeText.setText(joke.getUp()+"");
        holder.disagreeText.setText(joke.getDown()+"");
        holder.agreeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.agreeText.setText(joke.getUp() + 1 + "");
            }
        });
        holder.disagreeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.disagreeText.setText(joke.getDown()-1+"");
            }
        });
        holder.shareImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnekeyShare oks = new OnekeyShare();
                oks.setDialogMode();
                //关闭sso授权
                oks.disableSSOWhenAuthorize();
                oks.setText(joke.getContent().replaceAll("&hellip;", "."));
                oks.show(context);
            }
        });
        holder.collectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DBJoke dbJoke = new DBJoke();
                dbJoke.setId(joke.getId());
                dbJoke.setContent(joke.getContent().replaceAll("&hellip;","."));
                dbJoke.setTitle(joke.getTitle());
                try {
                    DbUtils.getDb().saveOrUpdate(dbJoke);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                Toast.makeText(context,"收藏成功!!",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setAction("joke");
                context.sendBroadcast(intent);
            }
        });
        return convertView;
    }

    private class ViewHolder extends BaseViewHolder{
        @ViewInject(R.id.iv_item_joke_agree)
        private ImageView agreeImage;
        @ViewInject(R.id.iv_item_joke_disagree)
        private ImageView disagreeImage;
        @ViewInject(R.id.iv_item_joke_share)
        private ImageView shareImage;
        @ViewInject(R.id.iv_item_joke_collect)
        private ImageView collectImage;
        @ViewInject(R.id.tv_item_joke_title)
        private TextView titleText;
        @ViewInject(R.id.tv_item_joke_body)
        private TextView bodyText;
        @ViewInject(R.id.tv_item_joke_agree)
        private TextView agreeText;
        @ViewInject(R.id.tv_item_joke_disagree)
        private TextView disagreeText;

        public ViewHolder(View convertView) {
            super(convertView);
            x.view().inject(this,convertView);
        }
    }
}
